package com.cloud.force.interfaces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cloud.force.api.bean.LawCauseItem;
import com.cloud.force.api.bean.LawCauseSearchCriteria;
import com.cloud.force.common.config.FeignConfiguration;
import com.github.pagehelper.PageInfo;

import feign.hystrix.FallbackFactory;

@FeignClient(value = "LAWCASE", configuration = FeignConfiguration.class, fallbackFactory = FeignClientLawCaseFallbackFactory.class)
public interface FeignClientLawCase {
	@RequestMapping(value = "lawcase/interface/causesearch", method = RequestMethod.POST)
	public PageInfo<LawCauseItem> lawCausePagination(@RequestBody LawCauseSearchCriteria criteria);
}

@Component
class FeignClientLawCaseFallbackFactory implements FallbackFactory<FeignClientLawCase> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientLawCaseFallbackFactory.class);
	
	@Override
	public FeignClientLawCase create(Throwable cause) {
		return new FeignClientLawCase(){
			@Override
			public PageInfo<LawCauseItem> lawCausePagination(LawCauseSearchCriteria criteria) {
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("lawCausePagination fallback; reason was:" + cause);
				}
				return null;
			}
			
		};
	}
	
}